package training.controller;
import javax.servlet.http.HttpSession;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.SessionAttributes;

import training.form.CustomerForm;
import training.service.CustomerService;
import training.vo.Customer;
import baytony.util.PagesUtil;
/**
 * @author fatsung
 *
 */
@Controller
@RequestMapping(value = "/cus")
@Transactional
@SessionAttributes("pages")
public class CustomerController {
	private static Logger log=LoggerFactory.getLogger(CustomerController.class);
	@Autowired
	@Qualifier("CustomerService")
	private CustomerService service;
	
	/**
	 * 預設產生首頁的畫面
	 * @param session
	 * @return
	 */
	@RequestMapping(method=RequestMethod.GET)
	public String index(ModelMap model){
		log.trace("{}.index() 程式啟動" ,this.getClass());
		PagesUtil<Customer> pages=new PagesUtil<Customer>(service.findAll());
		model.addAttribute("pages", pages);
 		return "cus/index";
	}
	/**
	 * 預設產生首頁的畫面
	 * @param session
	 * @return
	 */
	@RequestMapping(value = "/queryCustomer/{id}",method=RequestMethod.GET)
	public String getCustById(@PathVariable String id,ModelMap model){
		log.trace("{}.pathVariable() 程式啟動" ,this.getClass());
		model.remove("pages");
		model.addAttribute("customer", service.getCusById(id));
		return "cus/queryCustomer";
	}
	@RequestMapping(value = "/querySales/{id}",method=RequestMethod.GET)
	public String getSalesByCustomerId(@PathVariable String id,ModelMap model){
		log.trace("{}.pathVariable() 程式啟動" ,this.getClass());
		model.remove("pages");
		model.addAttribute("sales", service.getSalesByCustomerId(id));
		return "cus/querySales";
	}
	@RequestMapping(value = "/updCustomer",method=RequestMethod.POST)
	public String updateCust(CustomerForm cust,ModelMap model){
		log.trace("{}.pathVariable() 程式啟動" ,this.getClass());
		service.updateCus(cust.getId(),cust.getName(),cust.getPassword());
//		model.remove("pages");
		model.addAttribute("update","update completed");
		return "cus/updateCustomer";
	}
}
